import { _decorator, Component, Node, loader, SpriteFrame, Sprite, Texture2D, JsonAsset, Label, url } from 'cc';
const { ccclass, property, type } = _decorator;

@ccclass('MainUI')
export class MainUI extends Component {
    @type(Node)
    nodeS: Node = null;
    @type(Node)
    nodeT: Node = null;
    @type(Node)
    nodeBase: Node = null;
    @type(Node)
    nodeZip: Node = null;
    @type(Node)
    labelJson: Node = null;

    start() {
        // this.btnLoadSpriteFrame();
        // this.btnLoadTexture();
        this.btnLoadJson();
        // this.btnLoadBase64Png();
        // this.btnLoadZip();
    }
    update(deltaTime: number) {

    }

    /**
     * 加载json
     */
    btnLoadJson(): void {
        loader.loadRes("test", JsonAsset, (err, jsonRes) => {
            if (err) {
                console.log(err);
                return;
            }
            console.log(jsonRes)
            let json = jsonRes.json;
            this.labelJson.getComponent(Label).string = "公众号：" + json["gzh"] + "\n" + "微信号：" + json["wxh"] + "\n" + "QQ号：" + json["qqh"] + "\n" + "QQ群：" + json["qqq"];
        });
    }


    /**
     * 加载SpriteFrame
     */
    btnLoadSpriteFrame(): void {
        loader.loadRes("gzh_s/spriteFrame", SpriteFrame, (err: any, spriteFrame: SpriteFrame) => {
            if (err) {
                console.log(err);
                return;
            }
            this.nodeS.getComponent(Sprite).spriteFrame = spriteFrame;
        });
    }
    /**
     * 加载SpriteFrame
     */
    btnLoadTexture(): void {
        // 加载 texture
        loader.loadRes("gzh_t/texture", Texture2D, (err: any, texture: Texture2D) => {
            if (err) {
                console.log(err);
                return;
            }
            const spriteFrame = new SpriteFrame();
            spriteFrame.texture = texture;
            this.nodeT.getComponent(Sprite).spriteFrame = spriteFrame;
        });
    }
    /**
     * 加载base64 图片
     */
    btnLoadBase64Png(): void {
        let self = this;
        loader.loadRes("base64", JsonAsset, (err, jsonRes) => {
            if (err) {
                console.log(err);
                return;
            }
            let json = jsonRes.json;
            let base64: string = json["data"];

            let img = new Image();
            img.src = base64;
            let tex = new Texture2D();
            img.onload = function () {
                tex.reset({
                    width: img.width,
                    height: img.height,
                });
                tex.uploadData(img, 0, 0);
                tex.loaded = true;
                let spriteFrame = new SpriteFrame();
                spriteFrame.texture = tex;
                self.nodeBase.getComponent(Sprite).spriteFrame = spriteFrame;
            };
        });
    }
    /**
     * 加载
     */
    btnLoadZip(): void {
        let self = this;
        let path: string = url.raw("resources/gzh.zip");
        loader.load({ url: path, type: "binary", }, (err, res) => {
            if (err) return;
            JSZip.loadAsync(res).then((zip: JSZip) => {
                console.log(zip.files)
                zip.file("gzh_t.png").async("base64").then((data: string) => {
                    let img = new Image();
                    img.src ="data:image/jpg;base64,"+ data;
                    let tex = new Texture2D();
                    img.onload = function () {
                        tex.reset({
                            width: img.width,
                            height: img.height,
                        });
                        tex.uploadData(img, 0, 0);
                        tex.loaded = true;
                        let spriteFrame = new SpriteFrame();
                        spriteFrame.texture = tex;
                        self.nodeZip.getComponent(Sprite).spriteFrame = spriteFrame;
                    };
                })
            })
        });
    }




}
